package com.zx.onlineeducation.service.edu.controller;


import com.zx.onlineeducation.common.util.R;
import com.zx.onlineeducation.service.edu.entity.SubjectDO;
import com.zx.onlineeducation.service.edu.entity.vo.SubjectQueryVO;
import com.zx.onlineeducation.service.edu.entity.vo.SubjectTreeVO;
import com.zx.onlineeducation.service.edu.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程分类 前端控制器
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-12
 */
@Api(tags = "课程分类管理")
@RestController
@RequestMapping("/edu/subject")
@CrossOrigin // 解决跨域
public class SubjectController {

    /**
     * 课程分类服务接口
     */
    @Autowired
    private SubjectService subjectService;

    @ApiOperation("课程分类树形结构列表")
    @PostMapping("/tree")
    public R tree(
            @ApiParam(value = "课程分类查询对象")
            @RequestBody SubjectQueryVO subjectQueryVO) {
        List<SubjectTreeVO> tree = subjectService.getTree(subjectQueryVO);
        return R.ok().data("tree", tree);
    }

    @ApiOperation("添加课程分类")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "课程分类数据对象")
            @RequestBody SubjectDO subjectDO) {
        boolean save = subjectService.save(subjectDO);
        return save ? R.ok() : R.error();
    }

    @ApiOperation("根据ID查询课程分类")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(value = "课程分类ID", required = true)
            @PathVariable("id") String id) {
        SubjectDO subjectDO = subjectService.getById(id);
        return R.ok().data("subject", subjectDO);
    }

    @ApiOperation("修改课程分类")
    @PutMapping("/{id}")
    public R update(
            @ApiParam(value = "课程分类ID", required = true)
            @PathVariable("id") String id,
            @ApiParam(value = "课程分类数据对象", required = true)
            @RequestBody SubjectDO subjectDO) {
        subjectDO.setId(id);
        boolean update = subjectService.updateById(subjectDO);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("逻辑删除课程分类")
    @DeleteMapping("/{id}")
    public R remove(
            @ApiParam(value = "课程分类ID", required = true)
            @PathVariable("id") String id) {
        boolean remove = subjectService.remove(id);
        return remove ? R.ok() : R.error();
    }

    @ApiOperation("一级课程分类")
    @GetMapping("/one")
    public R getOneLevel() {
        List<SubjectDO> list = subjectService.getOneLevel();
        return R.ok().data("list", list);
    }

    @ApiOperation("二级课程分类")
    @GetMapping("/two/{parentId}")
    public R getTwoLevel(
            @ApiParam(value = "一级课程分类ID", required = true)
            @PathVariable("parentId") String parentId) {
        List<SubjectDO> list = subjectService.getTwoLevel(parentId);
        return R.ok().data("list", list);
    }

    @ApiOperation("Excel导入")
    @PostMapping("/import")
    public R save(
            @ApiParam(value = "excel文件", required = true)
            MultipartFile file) {
        subjectService.save(file);
        return R.ok();
    }
}

